package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbsj.core.page.PageEntity;
import com.hbsj.core.page.PageKit;
import com.hbsj.core.page.PageResult;
import com.hbsj.core.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ${package.Entity}.${entity};
import ${package.Exception}.${entity}DuplicateException;
import ${package.Exception}.${entity}NotFoundException;
import ${package.Mapper}.${table.mapperName};

import java.util.Date;
import java.util.List;
import java.math.BigDecimal;

@Service
public class ${entity}Service extends BaseService<${entity}> {
    @Autowired
    private ${entity}Mapper ${entity?uncap_first}Mapper;

    /**
     * 创建${table.comment}
     *
     * @return
     */
    @Transactional
    public ${entity} create(
<#list table.fieldsWithoutIdAndCU as field>
            ${field.propertyType} ${field.propertyName}<#if field_index != table.fieldsWithoutIdAndCU?size-1>,</#if>
</#list>
    ) {
        //判断${table.comment}是否已经存在
        boolean isDuplicate = this.isDuplicate(
                null,
                new QueryWrapper<${entity}>()
                        .eq(${entity}.TBL_NAME, name)
        );

        if (isDuplicate) {
            throw new ${entity}DuplicateException(String.format("${entity?uncap_first} name [%s] duplicate", name));
        }

        ${entity} ${entity?uncap_first} = new ${entity}();
<#list table.fieldsWithoutIdAndCU as field>
        ${entity?uncap_first}.set${field.capitalName}(${field.propertyName});
</#list>

        this.insert(${entity?uncap_first});

        return this.mapper.selectById(${entity?uncap_first}.getId());
    }

    /**
     * 更新${table.comment}
     *
     * @return
     */
    @Transactional
    public ${entity} update(
<#list table.fieldsWithoutCU as field>
            ${field.propertyType} ${field.propertyName}<#if field_index != table.fieldsWithoutCU?size-1>,</#if>
</#list>
    ) {
        // 判断${table.comment}是否存在
        ${entity} found${entity} = this.mapper.selectById(id);
        if (found${entity} == null) {
            throw new ${entity}NotFoundException(String.format("${entity?uncap_first}Id [%s] not found", id));
        }

        // 判断${table.comment}是否重复
        boolean isDuplicate = this.isDuplicate(
                id,
                new QueryWrapper<${entity}>()
                        .eq(${entity}.TBL_NAME, name)
        );

        if (isDuplicate) {
            throw new ${entity}DuplicateException(String.format("${entity} name [%s] duplicate, id is [%s]", name, id));
        }

        ${entity} ${entity?uncap_first} = new ${entity}();
<#list table.fieldsWithoutCU as field>
        ${entity?uncap_first}.set${field.capitalName}(${field.propertyName});
</#list>

        this.updateById(${entity?uncap_first});

        return this.mapper.selectById(${entity?uncap_first}.getId());
    }

    /**
     * 删除单个${table.comment}
     *
     * @param id
     * @return
     */
    @Transactional
    public void delete(long id) {
        this.mapper.deleteById(id);
    }

    /**
     * 获取单个${table.comment}
     *
     * @param id
     * @return
     */
    public ${entity} get(long id) {
        ${entity} ${entity?uncap_first} = this.mapper.selectById(id);

        return ${entity?uncap_first};
    }

    /**
     * 获取${table.comment}分页列表
     *
     * @param pageEntity
     * @return
     */
    public PageResult<${entity}> getPagelist(PageEntity pageEntity) {
        Page<${entity}> page = new Page<>(pageEntity.getPageNum(), pageEntity.getPageSize());

        List<${entity}> records = ${entity?uncap_first}Mapper.getPageList(page);

        return PageKit.toPageResult(page.getCurrent(), page.getTotal(), records);
    }
}
